2 * Copyright (c) 2019 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "unittest_common.h"
18 #import <XCTest/XCTest.h>
20 struct UDPSocket_struct
22 mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCoreReceive expects every UDPSocket_struct to begin with mDNSIPPort port
24 typedef struct UDPSocket_struct UDPSocket;
26 // This client request was generated using the following command: "dns-sd -Q 123server.dotbennu.com. A".
27 char test_query_any_msgbuf[35] = {
28 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x32, 0x33, 0x73, 0x65, 0x72, 0x76, 0x65,
29 0x72, 0x2e, 0x64, 0x6f, 0x74, 0x62, 0x65, 0x6e, 0x6e, 0x75, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x00,
33 // Modified for different scopes
34 char test_query_local_msgbuf[35] = {
35 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x31, 0x32, 0x33, 0x73, 0x65, 0x72, 0x76, 0x65,
36 0x72, 0x2e, 0x64, 0x6f, 0x74, 0x62, 0x65, 0x6e, 0x6e, 0x75, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x00,
40 char test_query_interface_msgbuf[35] = {
41 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x32, 0x33, 0x73, 0x65, 0x72, 0x76, 0x65,
42 0x72, 0x2e, 0x64, 0x6f, 0x74, 0x62, 0x65, 0x6e, 0x6e, 0x75, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x00,
46 // Variables associated with contents of the above uDNS message
47 mDNSlocal char test_domainname_cstr[] = "123server.dotbennu.com.";
49 mDNSlocal mDNSBool _TestCreateEtcHostsEntryWithInterfaceID(const domainname *domain, const struct sockaddr *sa, const domainname *cname, mDNSInterfaceID interfaceID, AuthHash *auth)
50 { // Copied from mDNSMacOSXCreateEtcHostsEntry
54 mDNSInterfaceID InterfaceID = mDNSInterface_LocalOnly;
59 LogMsg("_TestCreateEtcHostsEntryWithInterfaceID: ERROR!! name NULL");
64 LogMsg("_TestCreateEtcHostsEntryWithInterfaceID: ERROR!! sa and cname both NULL");
68 if (sa && sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
70 LogMsg("_TestCreateEtcHostsEntryWithInterfaceID: ERROR!! sa with bad family %d", sa->sa_family);
77 InterfaceID = interfaceID;
81 rrtype = (sa->sa_family == AF_INET ? kDNSType_A : kDNSType_AAAA);
83 rrtype = kDNSType_CNAME;
85 // Check for duplicates. See whether we parsed an entry before like this ?
86 namehash = DomainNameHashValue(domain);
87 ag = AuthGroupForName(auth, namehash, domain);
93 if (rr->resrec.rrtype == rrtype)
95 if (rrtype == kDNSType_A)
98 ip.NotAnInteger = ((struct sockaddr_in*)sa)->sin_addr.s_addr;
99 if (mDNSSameIPv4Address(rr->resrec.rdata->u.ipv4, ip) && InterfaceID == rr->resrec.InterfaceID)
101 LogInfo("_TestCreateEtcHostsEntryWithInterfaceID: Same IPv4 address and InterfaceID for name %##s ID %d", domain->c, IIDPrintable(InterfaceID));
105 else if (rrtype == kDNSType_AAAA)
108 ip6.l[0] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[0];
109 ip6.l[1] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[1];
110 ip6.l[2] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[2];
111 ip6.l[3] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[3];
112 if (mDNSSameIPv6Address(rr->resrec.rdata->u.ipv6, ip6) && InterfaceID == rr->resrec.InterfaceID)
114 LogInfo("_TestCreateEtcHostsEntryWithInterfaceID: Same IPv6 address and InterfaceID for name %##s ID %d", domain->c, IIDPrintable(InterfaceID));
118 else if (rrtype == kDNSType_CNAME)
120 if (SameDomainName(&rr->resrec.rdata->u.name, cname))
122 LogInfo("_TestCreateEtcHostsEntryWithInterfaceID: Same cname %##s for name %##s", cname->c, domain->c);
130 rr = (AuthRecord *) callocL("etchosts", sizeof(*rr));
131 if (rr == NULL) return mDNSfalse;
132 mDNS_SetupResourceRecord(rr, NULL, InterfaceID, rrtype, 1, kDNSRecordTypeKnownUnique, AuthRecordLocalOnly, FreeEtcHosts, NULL);
133 AssignDomainName(&rr->namestorage, domain);
137 rr->resrec.rdlength = sa->sa_family == AF_INET ? sizeof(mDNSv4Addr) : sizeof(mDNSv6Addr);
138 if (sa->sa_family == AF_INET)
139 rr->resrec.rdata->u.ipv4.NotAnInteger = ((struct sockaddr_in*)sa)->sin_addr.s_addr;
142 rr->resrec.rdata->u.ipv6.l[0] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[0];
143 rr->resrec.rdata->u.ipv6.l[1] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[1];
144 rr->resrec.rdata->u.ipv6.l[2] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[2];
145 rr->resrec.rdata->u.ipv6.l[3] = ((struct sockaddr_in6*)sa)->sin6_addr.__u6_addr.__u6_addr32[3];
150 rr->resrec.rdlength = DomainNameLength(cname);
151 rr->resrec.rdata->u.name.c[0] = 0;
152 AssignDomainName(&rr->resrec.rdata->u.name, cname);
154 rr->resrec.namehash = DomainNameHashValue(rr->resrec.name);
155 SetNewRData(&rr->resrec, mDNSNULL, 0); // Sets rr->rdatahash for us
156 LogInfo("_TestCreateEtcHostsEntryWithInterfaceID: Adding resource record %s ID %d", ARDisplayString(&mDNSStorage, rr), IIDPrintable(rr->resrec.InterfaceID));
157 InsertAuthRecord(&mDNSStorage, auth, rr);
161 mDNSlocal mStatus InitEtcHostsRecords(void)
163 mDNS *m = &mDNSStorage;
164 struct sockaddr_storage hostaddr;
168 mDNSPlatformMemZero(&newhosts, sizeof(AuthHash));
170 memset(&hostaddr, 0, sizeof(hostaddr));
171 get_ip("10.0.0.201", &hostaddr);
172 MakeDomainNameFromDNSNameString(&domain, "123server.dotbennu.com");
173 _TestCreateEtcHostsEntryWithInterfaceID(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, mDNSInterface_P2P, &newhosts);
175 memset(&hostaddr, 0, sizeof(hostaddr));
176 get_ip("10.0.0.202", &hostaddr);
177 MakeDomainNameFromDNSNameString(&domain, "123server.dotbennu.com");
178 mDNSMacOSXCreateEtcHostsEntry_ut(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, mDNSNULL, &newhosts);
180 memset(&hostaddr, 0, sizeof(hostaddr));
181 get_ip("10.0.0.203", &hostaddr);
182 MakeDomainNameFromDNSNameString(&domain, "123server.dotbennu.com");
183 _TestCreateEtcHostsEntryWithInterfaceID(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, primary_interfaceID, &newhosts);
185 UpdateEtcHosts_ut(&newhosts);
186 m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
189 return mStatus_NoError;
192 mDNSlocal mDNSs32 NumReplies(reply_state * reply)
195 reply_state * nextreply = reply;
196 while(nextreply) { result++; nextreply = nextreply->next;}
200 mDNSlocal mDNSBool HasReplyWithInterfaceIndex(reply_state * reply, mDNSu32 interfaceIndex)
202 mDNSBool result = mDNSfalse;
203 reply_state * nextreply = reply;
206 result = (ntohl(nextreply->rhdr[0].ifi) == interfaceIndex);
208 nextreply = nextreply->next;
213 @interface LocalOnlyWithInterfacesTest : XCTestCase
215 UDPSocket* local_socket;
216 request_state* client_request_message;}
219 @implementation LocalOnlyWithInterfacesTest
221 // The InitThisUnitTest() initializes the mDNSResponder environment as well as
222 // a DNSServer. It also allocates memory for a local_socket and client request.
223 // Note: This unit test does not send packets on the wire and it does not open sockets.
226 mDNSPlatformMemZero(&mDNSStorage, sizeof(mDNS));
228 // Init unit test environment and verify no error occurred.
229 mStatus result = init_mdns_environment(mDNStrue);
230 XCTAssertEqual(result, mStatus_NoError);
232 // Add one DNS server and verify it was added.
234 XCTAssertEqual(CountOfUnicastDNSServers(&mDNSStorage), 1);
236 AddDNSServerScoped_ut(primary_interfaceID, kScopeInterfaceID);
237 XCTAssertEqual(CountOfUnicastDNSServers(&mDNSStorage), 2);
239 // Populate /etc/hosts
240 result = InitEtcHostsRecords();
241 XCTAssertEqual(result, mStatus_NoError);
243 int count = LogEtcHosts_ut(&mDNSStorage);
244 XCTAssertEqual(count, 3);
246 // Create memory for a socket that is never used or opened.
247 local_socket = (UDPSocket *) mDNSPlatformMemAllocateClear(sizeof(*local_socket));
249 // Create memory for a request that is used to make this unit test's client request.
250 client_request_message = calloc(1, sizeof(request_state));
255 mDNS *m = &mDNSStorage;
256 request_state* req = client_request_message;
257 DNSServer *ptr, **p = &m->DNSServers;
261 reply_state *reply = req->replies;
262 req->replies = req->replies->next;
263 mDNSPlatformMemFree(reply);
265 mDNSPlatformMemFree(req);
267 mDNSPlatformMemFree(local_socket);
273 LogInfo("FinalizeUnitTest: Deleting server %p %#a:%d (%##s)", ptr, &ptr->addr, mDNSVal16(ptr->port), ptr->domain.c);
274 mDNSPlatformMemFree(ptr);
278 // This unit test tries 3 different local cache queries
279 // 1) Test the Any query does not receive the entry scoped to the primary interface, but does received the local and P2P entries
280 // 2) Test the LocalOnly query receives all the entries
281 // 3) Test the interface scoped query receives the interface scoped entry
282 - (void)testLocalOnlyWithInterfacesTestSeries
284 request_state* req = client_request_message;
286 // Verify Any index returns 2 results.
287 [self _executeClientQueryRequest: req andMsgBuf: test_query_any_msgbuf];
288 XCTAssertEqual(NumReplies(req->replies), 2);
289 XCTAssertTrue(HasReplyWithInterfaceIndex(req->replies, kDNSServiceInterfaceIndexP2P));
290 XCTAssertTrue(HasReplyWithInterfaceIndex(req->replies, kDNSServiceInterfaceIndexLocalOnly));
292 // Verify LocalOnly index returns 3 results.
293 [self _executeClientQueryRequest: req andMsgBuf: test_query_local_msgbuf];
294 XCTAssertEqual(NumReplies(req->replies), 3);
295 XCTAssertTrue(HasReplyWithInterfaceIndex(req->replies, kDNSServiceInterfaceIndexP2P));
296 XCTAssertTrue(HasReplyWithInterfaceIndex(req->replies, kDNSServiceInterfaceIndexLocalOnly));
297 XCTAssertTrue(HasReplyWithInterfaceIndex(req->replies, primary_interfaceID));
299 // Verify en0 index returns 1 result.
300 test_query_interface_msgbuf[7] = primary_interfaceID;
301 [self _executeClientQueryRequest: req andMsgBuf: test_query_interface_msgbuf];
302 XCTAssertEqual(NumReplies(req->replies), 1);
303 XCTAssertTrue(HasReplyWithInterfaceIndex(req->replies, primary_interfaceID));
307 // Simulate a uds client request by setting up a client request and then
308 // calling mDNSResponder's handle_client_request. The handle_client_request function
309 // processes the request and starts a query. This unit test verifies
310 // the client request and query were setup as expected. This unit test also calls
311 // mDNS_execute which determines the cache does not contain the new question's
313 - (void)_executeClientQueryRequest: (request_state*)req andMsgBuf: (char*)msgbuf
315 mDNS *const m = &mDNSStorage;
316 char *msgptr = msgbuf;
317 size_t msgsz = sizeof(test_query_local_msgbuf);
318 mDNSs32 min_size = sizeof(DNSServiceFlags) + sizeof(mDNSu32) + 4;
320 mStatus err = mStatus_NoError;
321 char qname_cstr[MAX_ESCAPED_DOMAIN_NAME];
323 // Process the unit test's client request
324 start_client_request(req, msgptr, msgsz, query_request, local_socket);
325 XCTAssertEqual(err, mStatus_NoError);
327 // Verify the request fields were set as expected
328 XCTAssertNil((__bridge id)req->next);
329 XCTAssertNil((__bridge id)req->primary);
330 XCTAssertEqual(req->sd, client_req_sd);
331 XCTAssertEqual(req->process_id, client_req_process_id);
332 XCTAssertFalse(strcmp(req->pid_name, client_req_pid_name));
333 XCTAssertEqual(req->validUUID, mDNSfalse);
334 XCTAssertEqual(req->errsd, 0);
335 XCTAssertEqual(req->uid, client_req_uid);
336 XCTAssertEqual(req->ts, t_complete);
337 XCTAssertGreaterThan((mDNSs32)req->data_bytes, min_size);
338 XCTAssertEqual(req->msgend, msgptr+msgsz);
339 XCTAssertNil((__bridge id)(void*)req->msgbuf);
340 XCTAssertEqual(req->hdr.version, VERSION);
341 XCTAssertNil((__bridge id)req->replies);
342 XCTAssertNotEqual(req->terminate, (req_termination_fn)0);
343 XCTAssertEqual(req->flags, kDNSServiceFlagsReturnIntermediates);
345 // Verify the query fields were set as expected
346 q = &req->u.queryrecord.op.q;
347 XCTAssertNotEqual(q, (DNSQuestion *)mDNSNULL);
350 XCTAssertEqual(q, m->Questions);
351 XCTAssertEqual(q, m->NewQuestions);
352 XCTAssertTrue(q->InterfaceID == mDNSInterface_Any || q->InterfaceID == primary_interfaceID);
356 XCTAssertEqual(q, m->LocalOnlyQuestions);
357 XCTAssertEqual(q, m->NewLocalOnlyQuestions);
358 XCTAssertEqual(q->InterfaceID, mDNSInterface_LocalOnly);
360 XCTAssertEqual(q->SuppressUnusable, mDNSfalse);
361 XCTAssertEqual(q->ReturnIntermed, mDNStrue);
362 XCTAssertEqual(q->Suppressed, mDNSfalse);
364 ConvertDomainNameToCString(&q->qname, qname_cstr);
365 XCTAssertFalse(strcmp(qname_cstr, test_domainname_cstr));
366 XCTAssertEqual(q->qnamehash, DomainNameHashValue(&q->qname));
368 XCTAssertEqual(q->flags, req->flags);
369 XCTAssertEqual(q->qtype, 1);
370 XCTAssertEqual(q->qclass, 1);
371 XCTAssertEqual(q->LongLived, 0);
372 XCTAssertEqual(q->ExpectUnique, mDNSfalse);
373 XCTAssertEqual(q->ForceMCast, 0);
374 XCTAssertEqual(q->TimeoutQuestion, 0);
375 XCTAssertEqual(q->WakeOnResolve, 0);
376 XCTAssertEqual(q->UseBackgroundTraffic, 0);
377 XCTAssertEqual(q->ValidationRequired, 0);
378 XCTAssertEqual(q->ValidatingResponse, 0);
379 XCTAssertEqual(q->ProxyQuestion, 0);
380 XCTAssertNotEqual((void*)q->QuestionCallback, (void*)mDNSNULL);
381 XCTAssertNil((__bridge id)q->DNSSECAuthInfo);
382 XCTAssertNil((__bridge id)(void*)q->DAIFreeCallback);
383 XCTAssertEqual(q->AppendSearchDomains, 0);
384 XCTAssertNil((__bridge id)q->DuplicateOf);
386 // Call mDNS_Execute to see if the new question, q, has an answer in the cache.
387 // It won't be yet because the cache is empty.
388 m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
391 // Verify mDNS_Execute processed the new question.
392 XCTAssertNil((__bridge id)m->NewQuestions);
393 XCTAssertNil((__bridge id)m->NewLocalOnlyQuestions);
394 XCTAssertEqual(m->rrcache_totalused, 0);
395 m->Questions = nil; // Reset